package com.learn.controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/eureka")
public class EurekaConsumerController {
    @Autowired
    private EurekaClient eurekaClient;
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/info/eurekaClient")
    public EurekaClient eurekaClient() {
        // 客户端配置信息
        EurekaClientConfig eurekaClientConfig = eurekaClient.getEurekaClientConfig();
        System.out.println("客户端配置信息:" + eurekaClientConfig);
        
        // 服务端应用列表
        Applications applications = eurekaClient.getApplications();
        System.out.println("服务端应用列表:" + applications);
        
        return this.eurekaClient;
    }
    
    @GetMapping("/info/discoveryClient")
    public DiscoveryClient discoveryClient() {
        // 获取服务注册列表
        List<String> services = discoveryClient.getServices();
        System.out.println("服务注册列表：" + services);
        
        // 获取某个具体服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("PROJECT-SERVER-DEPT");
        instances.forEach(item -> {
            System.out.println(item.getHost() + " : " + item.getPort() + " : " + item.getUri() + " : " + item.getServiceId());
        });
        
        return discoveryClient;
    }
}
